package com.example.gank.fragmet;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.core.base.MVPBaseFragment;
import com.android.core.utils.ToastUtils;
import com.example.gank.R;
import com.example.gank.adapter.WelfareGankAdapter;
import com.example.gank.bean.WelfareGankBean;
import com.example.gank.mvp.presenter.WelfarePresenter;
import com.example.gank.mvp.view.WelfareView;
import com.example.gank.widget.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class WelfareFragment extends MVPBaseFragment<WelfareView, WelfarePresenter> implements WelfareView, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.rv_welfare)
    RecyclerView rvWelfare;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    private List<WelfareGankBean> welfareGankBeans;
    private WelfareGankAdapter welfareGankAdapter;

    private int currPage = 1;

    public WelfareFragment() {
        // Required empty public constructor
    }

    public static WelfareFragment newInstance() {
        return new WelfareFragment();
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_welfare;
    }

    @Override
    protected void onInitView(Bundle savedInstanceState) {
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        rvWelfare.addItemDecoration(new DividerGridItemDecoration(mContext));
        welfareGankBeans = new ArrayList<>();
        rvWelfare.setLayoutManager(new GridLayoutManager(mContext, 2));
        welfareGankAdapter = new WelfareGankAdapter<>(mContext, R.layout.item_welfare_gank_rv, welfareGankBeans);
        rvWelfare.setAdapter(welfareGankAdapter);
        rvWelfare.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                /**
                 * 1的值表示是否能向上滚动，false表示已经滚动到底部
                 * -1的值表示是否能向下滚动，false表示已经滚动到顶部
                 */
                if (!recyclerView.canScrollVertically(1)) {
                    currPage++;
                    loadNetData();
                }
            }
        });
        lazyLoad();
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        loadNetData();
    }

    @Override
    protected WelfarePresenter createPresenter() {
        return new WelfarePresenter();
    }

    private void loadNetData() {
        mPresenter.getWelfare(currPage);
    }


    @Override
    public void onSucceed(List<WelfareGankBean> data) {
        mHasLoadedOnce = true;
        welfareGankBeans.addAll(data);
        welfareGankAdapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onFail(String err) {
        ToastUtils.showShort(err);
        swipeRefreshLayout.setRefreshing(false);
    }


    @Override
    public void onRefresh() {
        currPage = 1;
        welfareGankBeans.clear();
        loadNetData();
    }
}
